package start;

import decoder.ClientRtmpDecoder;
import handler.RtmpHandler;
import handler.RtmpOutHandler;
import rtmp.RtmpHandshake;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class RtmpPlay {
    public static void main(String[] args) {
        // 首先，netty通过ServerBootstrap启动服务端
        Bootstrap client = new Bootstrap();

        //第1步 定义线程组，处理读写和链接事件，没有了accept事件
        EventLoopGroup group = new NioEventLoopGroup();
        final EventLoopGroup videoGroup = new NioEventLoopGroup();
        client.group(group);

        //第2步 绑定客户端通道
        client.channel(NioSocketChannel.class);

        //第3步 给NIoSocketChannel初始化handler， 处理读写事件
        client.handler(new ChannelInitializer<NioSocketChannel>() {  //通道是NioSocketChannel
            @Override
            protected void initChannel(NioSocketChannel ch) throws Exception {
                //字符串编码器，一定要加在SimpleClientHandler 的上面
                ch.pipeline().addLast(new ClientRtmpDecoder(videoGroup));
                ch.pipeline().addLast(new RtmpOutHandler());
                ch.pipeline().addLast(new RtmpHandler());
            }
        });

        //连接服务器
        try {
            ChannelFuture future = client.connect("127.0.0.1", 9999).sync();
            RtmpHandshake rtmpHandshake = new RtmpHandshake();
            future.channel().writeAndFlush(Unpooled.copiedBuffer(rtmpHandshake.getShake()));
            System.err.println("发送数据");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        while (true) {

        }
    }
}
